package com.hfzy.ihk.web.linkcall.controller.setting.speakingSkill;

import com.hfzy.ihk.common.constants.GlobalStatic;
import com.hfzy.ihk.common.core.elasticsearch.EsQueryUtils;
import com.hfzy.ihk.common.util.date.DateUtils;
import com.hfzy.ihk.common.util.string.StringUtil;
import com.hfzy.ihk.common.web.WebResult;
import com.hfzy.ihk.facade.linkcall.entity.flow.config.SpeakingPackage;
import com.hfzy.ihk.facade.linkcall.enums.SpeakingPackageType;
import com.hfzy.ihk.web.linkcall.biz.flow.config.SpeakingPackageBiz;
import com.hfzy.ihk.web.linkcall.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;
import java.util.stream.Collectors;


/**
 *基础话术配置
 * */
@Controller
@RequestMapping("/speakingSkill")
public class speakingSkillController {
    @Autowired
    SpeakingPackageBiz speakingPackageBiz;


    @RequestMapping("/querySpeakingSkill")
    public String querySpeakingSkill(){
        return "page/speakingSkill/querySpeakingSkill";
    }

    @RequestMapping("/addSpeakingSkill")
    public String addSpeakingSkill(){
        return "page/speakingSkill/addSpeakingSkill";
    }

    @RequestMapping("/querySpeakingSkillDetail")
    public String querySpeakingSkillDetail(){
        return "page/speakingSkill/querySpeakingSkillDetail";
    }


    @RequestMapping("/querySpeakingPackage")
    @ResponseBody
    public WebResult querySpeakingPackage(@RequestParam Map<String,String> params){
        WebResult webResult = null;
        try{
            Page<SpeakingPackage> speakingPackagePage = speakingPackageBiz.getSpeakingPackageByPage(params);
//            webResult = new WebResult().setSuccessCharWebResult("",speakingPackagePage.getTotalElements(),speakingPackagePage.getContent());
        }catch (Exception e){
           // return  new WebResult().setExceptionCharWebResult(e.getMessage());
        }finally {
            if(webResult == null){
             //   return  new WebResult().setFailCharWebResult("");
            }
        }
        return webResult;
    }

    @RequestMapping("/viewSeakingPackage")
    @ResponseBody
    public WebResult viewSeakingPackage(@RequestParam String id,Model model){
        WebResult webResult = null;
        try {
            SpeakingPackage speakingPackage = speakingPackageBiz.getSpeakingPackageById(id);
            Map<Integer,String> userMap = UserUtils.getUserNameMap(speakingPackage.getCreateUserId());
            Map<String,Object> callTaskMap = new HashMap<>();
            callTaskMap.put("packName",speakingPackage.getPackageName());
            callTaskMap.put("createTime", DateUtils.toString(speakingPackage.getCreateTime(),DateUtils.DATE_FORMAT_DATEONLY));
            callTaskMap.put("packageType",speakingPackage.getPackageType());//任务状态
            callTaskMap.put("createUserName",userMap.get(speakingPackage.getCreateUserId())!=null?userMap.get(speakingPackage.getCreateUserId()):"系统管理员");//创建人
            callTaskMap.put("id",speakingPackage.getId());//已call量
            model.addAttribute("speakingPackage",speakingPackage);
        }catch (Exception e){
//            return  new WebResult().setExceptionCharWebResult(e.getMessage());
        }finally {
            if(webResult == null){
//                return  new WebResult().setFailCharWebResult("");
            }
        }
        return  webResult;
    }


    @RequestMapping("/addSpeakingPackage")
    @ResponseBody
    public WebResult addSpeakingPackage(@RequestParam Map<String,String> params){
        WebResult webResult = null;
        try{
            SpeakingPackage speakingPackage = new SpeakingPackage();
            speakingPackage.setPackageName(params.get("packageName"));
            speakingPackage.setPackageType(SpeakingPackageType.fromValue(params.get("packageType")));
            speakingPackage.setCreateUserId(11);
            speakingPackageBiz.addSpeakingPackage(speakingPackage);
        }catch (Exception e){
//            return  new WebResult().setExceptionCharWebResult(e.getMessage());
        }finally {
            if(webResult == null){
//                return  new WebResult().setFailCharWebResult("");
            }
        }
        return webResult;
    }

    public WebResult updateSpeakingPackage(@RequestParam Map<String,String> params){
        WebResult webResult  = null;
        try{
            Set<String> keySet = params.keySet();
            if(keySet.contains("id")){
                SpeakingPackage speakingPackage = speakingPackageBiz.getSpeakingPackageById(params.get("id"));
                if(speakingPackage!=null){
                    Iterator entries = params.entrySet().iterator();
                    while (entries.hasNext()) {
                        Map.Entry entry = (Map.Entry) entries.next();
                        String key = entry.getKey().toString();
                        switch (key) {
                            case "packageName":
                                speakingPackage.setPackageName(params.get("packageName"));
                                break;
                            case "packageType":
                                if(!StringUtil.isEmpty(params.get("packageType"))){
                                    speakingPackage.setPackageType(SpeakingPackageType.fromValue(params.get("packageType")));
                                }
                                break;
                            default:
                                break;
                        }
                    }
                    webResult = speakingPackageBiz.saveSpeakingPackage(speakingPackage);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
//            return  new WebResult().setExceptionCharWebResult(e.getMessage());
        }finally {
            if(webResult == null){
//                return  new WebResult().setFailCharWebResult("");
            }
        }
        return webResult;
    }

    public WebResult delSpeakingSkill(@RequestParam String id){
        WebResult webResult = null;
        try {
            webResult = speakingPackageBiz.deleteSpeakingPackageById(id);
        }catch (Exception e){
            e.printStackTrace();
//            return  new WebResult().setExceptionCharWebResult(e.getMessage());
        }finally {
            if(webResult == null){
//                return  new WebResult().setFailCharWebResult("");
            }
        }
        return  webResult;
    }



    @RequestMapping("/addFlow")
    public String addFlow(){
        return "page/speakingSkill/addFlow";
    }


    @RequestMapping("/flowSetting")
    public String flowSetting(){
        return "page/speakingSkill/flowSetting";
    }

}
